/*
 *    Copyright (c) 2018-2025, boss All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the boss developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: boss
 */

package com.hopes.bizz.line.pipe.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hopes.bizz.line.pipe.dto.CoordinateDTO;
import com.hopes.bizz.line.pipe.dto.LinePipeDTO;
import com.hopes.boss.common.core.util.R;
import com.hopes.boss.common.log.annotation.SysLog;
import com.hopes.bizz.line.pipe.entity.LinePipeEntity;
import com.hopes.bizz.line.pipe.dto.LinePipeExcelDTO;
import com.hopes.bizz.line.pipe.vo.LinePipeExcelVO;
import com.hopes.bizz.line.pipe.service.LinePipeService;
import org.springframework.security.access.prepost.PreAuthorize;
import com.hopes.boss.common.excel.annotation.RequestExcel;
import com.hopes.boss.common.excel.annotation.ResponseExcel;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import org.springframework.http.HttpHeaders;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import java.util.Collections;
import java.util.List;

/**
 * 管段
 *
 * @author pig
 * @date 2024-02-21 21:46:39
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/pipe" )
@Tag(description = "pipe" , name = "管段管理" )
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class LinePipeController {

    private final LinePipeService linePipeService;

    /**
     * 分页查询
     * @param page 分页对象
     * @param linePipe 管段
     * @return
     */
    @Operation(summary = "分页查询" , description = "分页查询" )
    @GetMapping("/page" )
    @PreAuthorize("@pms.hasPermission('line_pipe_view')" )
    public R getLinePipePage(Page page, LinePipeDTO linePipe) {
        LambdaQueryWrapper<LinePipeEntity> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(ObjUtil.isNotNull(linePipe.getPipelineId()), LinePipeEntity::getPipelineId, linePipe.getPipelineId());
        wrapper.like(StrUtil.isNotBlank(linePipe.getKeyword()), LinePipeEntity::getPipeCode, linePipe.getKeyword())
                .or().like(StrUtil.isNotBlank(linePipe.getKeyword()), LinePipeEntity::getStartNum, linePipe.getKeyword())
                .or().like(StrUtil.isNotBlank(linePipe.getKeyword()), LinePipeEntity::getEndNum, linePipe.getKeyword())
                .or().like(StrUtil.isNotBlank(linePipe.getKeyword()), LinePipeEntity::getFlowDirectionCode, linePipe.getKeyword());
        return R.ok(linePipeService.pageVo(page, wrapper));
    }


    /**
     * 列表查询
     * @param linePipe 管段
     * @return
     */
    @Operation(summary = "列表查询" , description = "列表查询" )
    @GetMapping("/list" )
    @PreAuthorize("@pms.hasPermission('line_pipe_view')" )
    public R getLinePipeList(LinePipeDTO linePipe) {
        final LambdaQueryWrapper<LinePipeEntity> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(ObjUtil.isNotNull(linePipe.getPipelineId()), LinePipeEntity::getPipelineId, linePipe.getPipelineId());
        wrapper.like(StrUtil.isNotBlank(linePipe.getKeyword()), LinePipeEntity::getPipeCode, linePipe.getKeyword())
                .or().like(StrUtil.isNotBlank(linePipe.getKeyword()), LinePipeEntity::getStartNum, linePipe.getKeyword())
                .or().like(StrUtil.isNotBlank(linePipe.getKeyword()), LinePipeEntity::getEndNum, linePipe.getKeyword())
                .or().like(StrUtil.isNotBlank(linePipe.getKeyword()), LinePipeEntity::getFlowDirectionCode, linePipe.getKeyword());
        return R.ok(linePipeService.list(wrapper));
    }


    /**
     * 通过id查询管段
     * @param id id
     * @return R
     */
    @Operation(summary = "通过id查询" , description = "通过id查询" )
    @GetMapping("/{id}" )
    @PreAuthorize("@pms.hasPermission('line_pipe_view')" )
    public R getById(@PathVariable("id" ) Long id) {
        return R.ok(linePipeService.getById(id));
    }

    /**
     * 新增管段
     * @param linePipe 管段
     * @return R
     */
    @Operation(summary = "新增管段" , description = "新增管段" )
    @SysLog("新增管段" )
    @PostMapping
    @PreAuthorize("@pms.hasPermission('line_pipe_add')" )
    public R save(@RequestBody LinePipeEntity linePipe) {
        return R.ok(linePipeService.save(linePipe));
    }


    /**
     * 地图选框查询管段
     * @param coordinateList 管段
     * @return R
     */
    @Operation(summary = "地图选框查询管段" , description = "地图选框查询管段" )
    @SysLog("地图选框查询管段" )
    @PostMapping("/queryByArea")
    @PreAuthorize("@pms.hasPermission('line_pipe_view')" )
    public R queryByArea(@RequestBody List<CoordinateDTO> coordinateList) {
        return R.ok(linePipeService.queryByArea(coordinateList));
    }

    /**
     * 修改管段
     * @param linePipe 管段
     * @return R
     */
    @Operation(summary = "修改管段" , description = "修改管段" )
    @SysLog("修改管段" )
    @PutMapping
    @PreAuthorize("@pms.hasPermission('line_pipe_edit')" )
    public R updateById(@RequestBody LinePipeEntity linePipe) {
        return R.ok(linePipeService.updateById(linePipe));
    }

    /**
     * 通过id删除管段
     * @param ids id列表
     * @return R
     */
    @Operation(summary = "通过id删除管段" , description = "通过id删除管段" )
    @SysLog("通过id删除管段" )
    @DeleteMapping
    @PreAuthorize("@pms.hasPermission('line_pipe_del')" )
    public R removeById(@RequestBody Long[] ids) {
        return R.ok(linePipeService.removeBatchByIds(CollUtil.toList(ids)));
    }




    /**
     * excel模板
     *
     * @return excel 文件流
     */
    @ResponseExcel
    @GetMapping("/template")
    @PreAuthorize("@pms.hasPermission('line_pipe_export')")
    public List<LinePipeExcelVO> template() {
        return linePipeService.exportExcel(Collections.singletonList(new LinePipeEntity()));
    }

    /**
     * 导出excel 表格
     * @param linePipe 查询条件
     * @return excel 文件流
     */
    @ResponseExcel
    @GetMapping("/export")
    @PreAuthorize("@pms.hasPermission('line_pipe_export')")
    public List<LinePipeExcelVO> export(LinePipeDTO linePipe) {
        LambdaQueryWrapper<LinePipeEntity> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(ObjUtil.isNotNull(linePipe.getPipelineId()), LinePipeEntity::getPipelineId, linePipe.getPipelineId());
        wrapper.like(StrUtil.isNotBlank(linePipe.getKeyword()), LinePipeEntity::getPipeCode, linePipe.getKeyword())
                .or().like(StrUtil.isNotBlank(linePipe.getKeyword()), LinePipeEntity::getStartNum, linePipe.getKeyword())
                .or().like(StrUtil.isNotBlank(linePipe.getKeyword()), LinePipeEntity::getEndNum, linePipe.getKeyword())
                .or().like(StrUtil.isNotBlank(linePipe.getKeyword()), LinePipeEntity::getFlowDirectionCode, linePipe.getKeyword());
        return linePipeService.exportExcel(linePipeService.list(wrapper));
    }

    /**
     * 导入excel 表格
     * @param linePipeExcelDtoList 查询条件
     * @param bindingResult 错误信息列表
     * @return ok fail
     */
    @PostMapping("/import")
    @PreAuthorize("@pms.hasPermission('line_pipe_export')")
    public R importExcel(@RequestExcel List<LinePipeExcelDTO> linePipeExcelDtoList, BindingResult bindingResult) {
        return linePipeService.importExcel(linePipeExcelDtoList, bindingResult);
    }
}
